/**
 * "Visual Paradigm: DO NOT MODIFY THIS FILE!"
 * 
 * This is an automatic generated file. It will be regenerated every time 
 * you generate persistence class.
 * 
 * Modifying its content may cause the program not work, or your work may lost.
 */

/**
 * Licensee: DuKe TeAm
 * License Type: Purchased
 */
package orm.impl;

import org.orm.*;
import org.hibernate.Query;
import java.util.List;
import orm.*;

public class ProfesionalDAOImpl implements orm.dao.ProfesionalDAO {
	public Profesional[] listProfesionalByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return listProfesionalByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional[] listProfesionalByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return listProfesionalByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional[] listProfesionalByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Profesional as Profesional");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			List list = query.list();
			return (Profesional[]) list.toArray(new Profesional[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional[] listProfesionalByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Profesional as Profesional");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			List list = query.list();
			return (Profesional[]) list.toArray(new Profesional[list.size()]);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional loadProfesionalByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return loadProfesionalByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional loadProfesionalByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return loadProfesionalByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional loadProfesionalByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		Profesional[] profesionals = listProfesionalByQuery(session, condition, orderBy);
		if (profesionals != null && profesionals.length > 0)
			return profesionals[0];
		else
			return null;
	}
	
	public Profesional loadProfesionalByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		Profesional[] profesionals = listProfesionalByQuery(session, condition, orderBy, lockMode);
		if (profesionals != null && profesionals.length > 0)
			return profesionals[0];
		else
			return null;
	}
	
	public static java.util.Iterator iterateProfesionalByQuery(String condition, String orderBy) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return iterateProfesionalByQuery(session, condition, orderBy);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateProfesionalByQuery(String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		try {
			PersistentSession session = orm.BabyManagerPersistentManager.instance().getSession();
			return iterateProfesionalByQuery(session, condition, orderBy, lockMode);
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateProfesionalByQuery(PersistentSession session, String condition, String orderBy) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Profesional as Profesional");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public static java.util.Iterator iterateProfesionalByQuery(PersistentSession session, String condition, String orderBy, org.hibernate.LockMode lockMode) throws PersistentException {
		StringBuffer sb = new StringBuffer("From orm.Profesional as Profesional");
		if (condition != null)
			sb.append(" Where ").append(condition);
		if (orderBy != null)
			sb.append(" Order By ").append(orderBy);
		try {
			Query query = session.createQuery(sb.toString());
			query.setLockMode("this", lockMode);
			return query.iterate();
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional createProfesional() {
		return new orm.Profesional();
	}
	
	public boolean save(orm.Profesional profesional) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().saveObject(profesional);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean delete(orm.Profesional profesional) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().deleteObject(profesional);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Profesional profesional)throws PersistentException {
		try {
			orm.Encuesta idPrefesional = profesional.getIdPrefesional();
			if(profesional.getIdPrefesional() != null) {
				profesional.getIdPrefesional().setProfesional(null);
			}
			profesional.setIdPrefesional(idPrefesional);
			
			orm.Encuesta[] lEncuestaIdEncuestas = profesional.encuestaIdEncuesta.toArray();
			for(int i = 0; i < lEncuestaIdEncuestas.length; i++) {
				lEncuestaIdEncuestas[i].profesionalIdPrefesional.remove(profesional);
			}
			return delete(profesional);
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean deleteAndDissociate(orm.Profesional profesional, org.orm.PersistentSession session)throws PersistentException {
		try {
			orm.Encuesta idPrefesional = profesional.getIdPrefesional();
			if(profesional.getIdPrefesional() != null) {
				profesional.getIdPrefesional().setProfesional(null);
			}
			profesional.setIdPrefesional(idPrefesional);
			
			orm.Encuesta[] lEncuestaIdEncuestas = profesional.encuestaIdEncuesta.toArray();
			for(int i = 0; i < lEncuestaIdEncuestas.length; i++) {
				lEncuestaIdEncuestas[i].profesionalIdPrefesional.remove(profesional);
			}
			try {
				session.delete(profesional);
				return true;
			} catch (Exception e) {
				return false;
			}
		}
		catch(Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean refresh(orm.Profesional profesional) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().getSession().refresh(profesional);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public boolean evict(orm.Profesional profesional) throws PersistentException {
		try {
			orm.BabyManagerPersistentManager.instance().getSession().evict(profesional);
			return true;
		}
		catch (Exception e) {
			e.printStackTrace();
			throw new PersistentException(e);
		}
	}
	
	public Profesional loadProfesionalByCriteria(ProfesionalCriteria profesionalCriteria) {
		Profesional[] profesionals = listProfesionalByCriteria(profesionalCriteria);
		if(profesionals == null || profesionals.length == 0) {
			return null;
		}
		return profesionals[0];
	}
	
	public Profesional[] listProfesionalByCriteria(ProfesionalCriteria profesionalCriteria) {
		return profesionalCriteria.listProfesional();
	}
}
